import { _decorator, Node, Button, Label, Vec3, v3 } from 'cc';
import { BasePanel } from '../../../../scripts/libs/ui/BasePanel';
import { BindButton } from '../../../../scripts/libs/rxjs/cc3/BindButton';
import { MergeASAP } from '../../../../scripts/libs/rxjs/cc3/Merge';
import { udm } from '../../../../scripts/managers/UserDataManager';
import { lm } from '../../LobbyManager';
import { adsm } from '../../../../scripts/managers/AdsManager';
import { gm } from '../../../../scripts/managers/GameManager';
const { ccclass, property } = _decorator;

@ccclass('LoseLifeTipPanel')
export class LoseLifeTipPanel extends BasePanel {

    @property(Node)
    mainNode: Node = null;

    @property(Button)
    cancelButton: Button = null;

    @property(Button)
    closeButton: Button = null;

    @property(Button)
    leaveButton: Button = null;

    @property(Node)
    heartInfNode: Node = null;

    @property(Node)
    heartMinusNode: Node = null;

    @property(Label)
    heartLabel: Label = null;

    willBind(): void {
        lm.loseLifeTipPanelOpened.value = true;
        this.adaptive();
    }

    useObserves() {
        return [
            BindButton(this.closeButton).subscribe(this.cancelButtonClicked.bind(this)),
            BindButton(this.cancelButton).subscribe(this.cancelButtonClicked.bind(this)),
            BindButton(this.leaveButton).subscribe(this.leaveButtonClicked.bind(this)),

            MergeASAP(udm.heart, udm.infHeartTill).subscribe(this.refresh.bind(this)),
            // IntervalStartNow(1, this.node).subscribe(_ => {
            //     this.refresh();
            // }),
        ];
    }

    adaptive() {
        this.mainNode.setScale(v3(Vec3.ONE).multiplyScalar(lm.getAdaptiveScale()));
    }

    onDestroyView(): void {
        lm.loseLifeTipPanelOpened.value = false;
    }

    refresh() {
        let remainMsInfHeart = udm.getRemainMsToOverInfHeart();
        if (remainMsInfHeart > 0) { // 在无限体力状态下

            this.heartInfNode.active = true;
            this.heartMinusNode.active = false;

            // this.contentLabel.string = `Heart: Inf(${MSRToString_th_m_s_2(MSResolve(remainMsInfHeart))})`;
            // this.leaveButtonLabel.string = 'Give Up';
        } else {
            this.heartInfNode.active = false;
            this.heartMinusNode.active = true;

            let remainMs = udm.getRemainMsToRecoverOneHeart();
            if (remainMs != null) {
                // let msr = MSResolve(remainMs);
                // this.contentLabel.string = `Heart: ${udm.getHeartCount()}(${MSRToString_tm_s(msr)})`;

                this.heartLabel.string = `${udm.getHeartCount()}`;

            } else {

                // this.heartLabel.string = 'MAX';
                this.heartLabel.string = `${udm.getHeartCount()}`;

                // this.contentLabel.string = `Heart: MAX`;
            }
            // this.leaveButtonLabel.string = `(Heart:-1) Give Up`;
        }
    }

    cancelButtonClicked() {
        LoseLifeTipPanel.CloseProcess(null, 'cancel');
    }

    leaveButtonClicked() {
        LoseLifeTipPanel.CloseProcess(null, 'leave');

        adsm.tryShowInterstitialAds(gm.stageInfo.value, 'AdQuit');
    }
}

